package com.qdu.dao.impl;

import com.qdu.dao.UserDao;
import com.qdu.model.User;
import com.qdu.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public boolean addUser(User user) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DatabaseUtil.getConnection();
            String sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getEmail());
            stmt.setString(3, user.getPassword());
            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DatabaseUtil.close((ResultSet) conn, stmt, null);
        }
    }

    @Override
    public User getUserByUsername(String username) {
        // 实现根据用户名查找用户的数据库操作
        return null;
    }

    @Override
    public User getUserByEmail(String email) {
        // 实现根据用户邮箱查找用户的数据库操作
        return null;
    }

    @Override
    public boolean validateUser(String username, String password) {
        // 实现验证用户登录的数据库操作
        return false;
    }
}
